package spotGUI.mainFrameGUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import managers.DBManager;
import spotGUI.utils.UserListCellRenderer;
import tests.StartupManager;
import utils.MaxLenghtTextDocument;
import utils.LoginRetrievingMethods;
import utils.StackTracePrinter;
import java.util.ResourceBundle;

public class LoginDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField nicknameField;
	private JComboBox comboBox;
	private JLabel lblName;
	private JButton btnLogin;
	private JLabel lblNickname;
	private JLabel lblPassword;
	private JPasswordField passwordField;
	
	private Object[] pswd;
	private JLabel image;
	private JLabel info;
	private JLabel logBar;


	public LoginDialog(final StartupManager init) {
		
		setTitle(ResourceBundle.getBundle("spotGUI.mainFrameGUI.messages").getString("LoginDialog.this.title")); //$NON-NLS-1$ //$NON-NLS-2$
		setSize(new Dimension(463, 390));
		setLocationRelativeTo(null);
		
		setResizable(false);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setModal(true);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(null);
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		GridBagLayout gbl_contentPanel = new GridBagLayout();
		gbl_contentPanel.columnWidths = new int[]{198, 15, 188, 11, 0};
		gbl_contentPanel.rowHeights = new int[]{12, 0, 0, 0, 0, 70, 132, 0};
		gbl_contentPanel.columnWeights = new double[]{1.0, 1.0, 1.0, 0.0, Double.MIN_VALUE};
		gbl_contentPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		contentPanel.setLayout(gbl_contentPanel);

		lblName = new JLabel(ResourceBundle.getBundle("spotGUI.mainFrameGUI.messages").getString("LoginDialog.lblName.text")); //$NON-NLS-1$ //$NON-NLS-2$
		GridBagConstraints gbc_lblName = new GridBagConstraints();
		gbc_lblName.anchor = GridBagConstraints.EAST;
		gbc_lblName.insets = new Insets(0, 0, 5, 5);
		gbc_lblName.gridx = 0;
		gbc_lblName.gridy = 1;
		contentPanel.add(lblName, gbc_lblName);

		comboBox = new JComboBox();
		comboBox.setRenderer(new UserListCellRenderer());
		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.insets = new Insets(0, 0, 5, 5);
		gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox.gridx = 2;
		gbc_comboBox.gridy = 1;
		contentPanel.add(comboBox, gbc_comboBox);

		lblNickname = new JLabel(ResourceBundle.getBundle("spotGUI.mainFrameGUI.messages").getString("LoginDialog.lblNickname.text")); //$NON-NLS-1$ //$NON-NLS-2$
		GridBagConstraints gbc_lblNickname = new GridBagConstraints();
		gbc_lblNickname.anchor = GridBagConstraints.EAST;
		gbc_lblNickname.insets = new Insets(0, 0, 5, 5);
		gbc_lblNickname.gridx = 0;
		gbc_lblNickname.gridy = 2;
		contentPanel.add(lblNickname, gbc_lblNickname);

		nicknameField = new JTextField();
		nicknameField.setDocument(new MaxLenghtTextDocument(25));
		GridBagConstraints gbc_nicknameField = new GridBagConstraints();
		gbc_nicknameField.insets = new Insets(0, 0, 5, 5);
		gbc_nicknameField.fill = GridBagConstraints.HORIZONTAL;
		gbc_nicknameField.gridx = 2;
		gbc_nicknameField.gridy = 2;
		contentPanel.add(nicknameField, gbc_nicknameField);
		nicknameField.setColumns(10);

		btnLogin = new JButton(ResourceBundle.getBundle("spotGUI.mainFrameGUI.messages").getString("LoginDialog.btnLogin.text")); //$NON-NLS-1$ //$NON-NLS-2$
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (checkInput()) {
					if ((Boolean)pswd[1] == true)
						init.setState(StartupManager.ACCEPTED);
					else
						init.setState(StartupManager.FIRST_RUN);
					init.setUserId((Integer)(pswd[2]));
					dispose();
				} else {
					logBar.setText(ResourceBundle.getBundle("spotGUI.mainFrameGUI.messages").getString("LoginDialog.1"));
				}
			}
		});

		lblPassword = new JLabel(ResourceBundle.getBundle("spotGUI.mainFrameGUI.messages").getString("LoginDialog.lblPassword.text")); //$NON-NLS-1$ //$NON-NLS-2$
		GridBagConstraints gbc_lblPassword = new GridBagConstraints();
		gbc_lblPassword.anchor = GridBagConstraints.EAST;
		gbc_lblPassword.insets = new Insets(0, 0, 5, 5);
		gbc_lblPassword.gridx = 0;
		gbc_lblPassword.gridy = 3;
		contentPanel.add(lblPassword, gbc_lblPassword);

		passwordField = new JPasswordField();
		passwordField.setDocument(new MaxLenghtTextDocument(20));
		GridBagConstraints gbc_passwordField = new GridBagConstraints();
		gbc_passwordField.insets = new Insets(0, 0, 5, 5);
		gbc_passwordField.fill = GridBagConstraints.HORIZONTAL;
		gbc_passwordField.gridx = 2;
		gbc_passwordField.gridy = 3;
		contentPanel.add(passwordField, gbc_passwordField);
		
		logBar = new JLabel("");
		logBar.setFont(new Font("Dialog", Font.BOLD, 12));
		logBar.setForeground(Color.red.darker().darker());
		GridBagConstraints gbc_logBar = new GridBagConstraints();
		gbc_logBar.anchor = GridBagConstraints.EAST;
		gbc_logBar.insets = new Insets(0, 0, 5, 5);
		gbc_logBar.gridx = 0;
		gbc_logBar.gridy = 4;
		contentPanel.add(logBar, gbc_logBar);
		GridBagConstraints gbc_btnLogin = new GridBagConstraints();
		gbc_btnLogin.anchor = GridBagConstraints.EAST;
		gbc_btnLogin.insets = new Insets(0, 0, 5, 5);
		gbc_btnLogin.gridx = 2;
		gbc_btnLogin.gridy = 4;
		contentPanel.add(btnLogin, gbc_btnLogin);
		
		try {
			comboBox.setModel(new DefaultComboBoxModel(LoginRetrievingMethods.getUsers(DBManager.getConnection(), null)));
			comboBox.setSelectedIndex(-1);
			
			info = new JLabel(ResourceBundle.getBundle("spotGUI.mainFrameGUI.messages").getString("LoginDialog.2"));
			info.setBorder(new EmptyBorder(0, 15, 0, 0));
			GridBagConstraints gbc_info = new GridBagConstraints();
			gbc_info.anchor = GridBagConstraints.WEST;
			gbc_info.gridwidth = 3;
			gbc_info.insets = new Insets(0, 0, 5, 5);
			gbc_info.gridx = 0;
			gbc_info.gridy = 5;
			contentPanel.add(info, gbc_info);
			
			image = new JLabel("");
			image.setIcon(new ImageIcon(LoginDialog.class.getResource("/images/GUIBackgrounds/wavesForLogin.png")));
			GridBagConstraints gbc_image = new GridBagConstraints();
			gbc_image.gridwidth = 4;
			gbc_image.gridx = 0;
			gbc_image.gridy = 6;
			contentPanel.add(image, gbc_image);
		} catch (SQLException e) {
			StackTracePrinter.print(e);
			JOptionPane.showMessageDialog(this, ResourceBundle.getBundle("spotGUI.mainFrameGUI.messages").getString("LoginDialog.3"),
					ResourceBundle.getBundle("spotGUI.mainFrameGUI.messages").getString("LoginDialog.4"), JOptionPane.ERROR_MESSAGE);
			System.exit(-1);
		}	

	}
	
	private boolean checkInput () {

		

		if (comboBox.getSelectedIndex() == -1 && nicknameField.getText().length() == 0) {
			return false;
		}

		if (passwordField.getPassword().length == 0) {
			return false;
		} else {
			String password = new String(passwordField.getPassword());
			try {
				if (comboBox.getSelectedIndex() != -1) {
					pswd = LoginRetrievingMethods.getPassword(DBManager.getConnection(),
							Integer.toString((Integer)((Object[])comboBox.getSelectedItem())[0]), LoginRetrievingMethods.ID);
					if (!(password.equals(pswd[0]))) {
						return false;
					}
				} else {
					pswd = LoginRetrievingMethods.getPassword(DBManager.getConnection(),
							nicknameField.getText(), LoginRetrievingMethods.NICKNAME);
					if (nicknameField.getText().length() != 0) {
						if (!(password.equals(pswd[0]))) {
							return false;
						}
					}
				}
			} catch (SQLException e) {
				StackTracePrinter.print(e);
				return false;
			}
		}
		return true;
	}
}
